<?php
	include_once 'isLogin.php';

	$table_name = "company_order";
	$key_index = array('title','content','upid','state');
	
	$colors = array(
				'state'=>array('0'=>'#ff0000','1'=>'#16d316'),
			);
	$names = array(
				'state'=>array('0'=>'禁用','1'=>'可用'),
			);
	//查询
         
	if($_GET['submit'] === '搜索'){

		$name=$_GET['name'];
	       
		if($_GET[name]!='' && $_GET[name]!=null){
			$select_sql="  and p.name like '%".$name."%'";
		}
		
	   }

	//添加
//	if($_GET['action']=='add'){
//		
//		//插入数据
//		if(is_array($_POST['values'])){
//			foreach($_POST['values'] as $k=>$v){
//				$arr[$key_index[$k]] = $v;
//			}
//		}
//		$commonc->insert($table_name,$arr);
//		$id = $commonc->insert_id();
//		
//		//排序
//		$arr = array('sort'=>$id);
//		$commonc->update($table_name, $arr, "id='".$id."'");
//		
//		//查询记录
//		$data = $commonc->getInfo($table_name, $id);
//		
//		if($id!=''){
//			$return='<tr line_id="'.$id.'">
//			<td> <input type="checkbox" name="checkbox" value="'.$id.'" /></td>
//			<td value_id="'.$id.'">'.$data['title'].'</td>
//			<td value_id="'.$id.'">'.$data['content'].'</td>
//			<td>&nbsp;</td>
//			<td class="xiugai">
//				<a href="javascript:;" onclick="sort($(this),\''.$id.'\',\'up\',\''.$_basename.'?action=sort\')"><img src="images/sort-up.png"/></a>
//				<a href="javascript:;" onclick="sort($(this),\''.$id.'\',\'down\','.$_basename.'?action=sort\')"><img src="images/sort-down.png"/></a>
//			</td>
//			<td class="xiugai"><a href="javascript:;" style="color: '.$colors['state'][$data['state']].'" onclick="state(\''.$id.'\',\'state\',\''.$_basename.'\',$(this))">'.$names['state'][$data['state']].'</a></td>
//			<td class="xiugai">
//			<a href="javascript:;" onclick="edit(\''.$id.'\',$(this),\''.$_basename.'?action=edit\')">修改</a>';
//			if($data['upid']==false) $return.='<a href="'.$_basename.'?id='.$id.'">子类</a>';
//			$return.='</td></tr>';
//			$arr=array('error'=>'0','content'=>$return);
//		}else{
//			$arr=array('error'=>'1','content'=>'添加失败');
//		}
//		echo json_encode($arr);
//		exit();
//	}
//	
	//删除
	if($_GET['action']=='del'){
		$delFile = array(
				array('key'=>'img','dir'=>'../upload/test/large/'),
				array('key'=>'img','dir'=>'../upload/test/middle/'),
				array('key'=>'img','dir'=>'../upload/test/small/'),
				array('key'=>'file','dir'=>'../upload/test/file/'),
		);
		echo $commonc->del($table_name,$_POST['id'],$delFile);
		exit();
	}
	
	//修改
	if($_GET['action']=='edit'){
		$arr = array($key_index[$_POST['index']]=>$_POST['value']);
		echo $commonc->update($table_name,$arr,"id='".$_POST['value_id']."'");
		exit();
	}
	
	//修改状态
	if($_GET['action']=='state'){
		echo $commonc->changeState($table_name,$_POST['id'],$_POST['key'],$colors[$_POST['key']],$names[$_POST['key']]);
		exit();
	}
	
	//排序
	if($_GET['action']=='sort'){
		if($_POST['type']=='down') $type = "prev";
		if($_POST['type']=='up') $type = "next";
		$sort = $commonc->sortChange($table_name, $_POST['id'], $type);
		echo $sort['id'];
		exit();
	}

	//得到所有内容 DATE_FROMAT(o.order_date,'%Y-%m')
	if($_GET['id']!=''){
                $date = date('Y-m',strtotime("-1 month"));
                if($_GET['date']!='')
                {
                    $date = $_GET['date'];
                }
                
		$where = " where DATE_FORMAT(o.order_date,'%Y-%m') = '". $date."' and p.company_id = '".$_GET['id']."'".$select_sql;
	        $sql = $table_name." o  left  join company_product p  on o.product_id=p.id ";
	        $sql.= $where;
                $key = " p.name,p.price,DATE_FORMAT(o.order_date,'%Y-%m-%d') as order_date,o.amount,o.pay_money,o.trading_status";
	        $row=$commonc->getList($sql,$sql,$key);
                if(is_array($row['result']) )
                {
                    foreach ($row['result'] as $k => $v) {
                        if($v['trading_status']==='2')
                        {
                            $v['trading_status'] = '成功';
                        } 
                         if($v['trading_status']==='1')
                        {
                            $v['trading_status'] = '进行中';
                        } 
                         if($v['trading_status']==='3')
                        {
                            $v['trading_status'] = '失败';
                        } 
                        $row['result'][$k] = $v;
                    }
                }
	}
        
        
	
	$smarty->assign("row",$row['result']);
        $smarty->assign("select_name",$name);
        $smarty->assign("company_id",$_GET['id']);
       
	$smarty->assign('page',$row['page']);
	$smarty->display($_filename.".html");
	
?>